package com.nuc.seckill.base;


import com.nuc.seckill.exception.ErrorMessage;
import lombok.Data;

import java.io.Serializable;

/**
 * @author spider
 */
@Data
public class BaseResponse<T> implements Serializable {
    public static final BaseResponse<?> OK = new BaseResponse<>(0, "成功", "");
    private Integer code;
    private String message;
    private T data;

    public BaseResponse(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public static <T> BaseResponse<T> ok(T t) {
        return new BaseResponse<>(0, "成功", t);
    }

    public static BaseResponse<?> error(ErrorMessage errorMessage) {
        return new BaseResponse<>(errorMessage.getCode(), errorMessage.getMessage(), null);
    }

    public static <T> BaseResponse<T> error(ErrorMessage errorMessage, T data) {
        return new BaseResponse<>(errorMessage.getCode(), errorMessage.getMessage(), data);
    }

    public static BaseResponse<?> error(Integer code, String message) {
        return new BaseResponse<>(code, message, null);
    }

    public static <T> BaseResponse<T> error(Integer code, String message, T data) {
        return new BaseResponse<>(code, message, data);
    }
}
